using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Pampa.Grid.Core
{
    public class ColumnConfig
    {
        private string field;
        private bool visible;
        private OrderDirection order;

        public string Field
        {
            get { return field; }
            set { field = value; }
        }

        public bool Visible
        {
            get { return visible; }
            set { visible = value; }
        }

        public OrderDirection Order
        {
            get { return order; }
            set { order = value; }
        }

        #region Xml

        public static ColumnConfig FromXml(XmlNode node)
        {
            ColumnConfig column = new ColumnConfig();

            if (node.Attributes["Field"] == null)
            {
                column.Field = node.Attributes["field"].Value;
                column.Visible = bool.Parse(node.Attributes["visible"].Value);
                column.Order = (OrderDirection)Enum.Parse(typeof(OrderDirection), node.Attributes["order"].Value);
            }
            else
            {
                column.Field = node.Attributes["Field"].Value;
                column.Visible = bool.Parse(node.Attributes["Visible"].Value);
                column.Order = (OrderDirection)Enum.Parse(typeof(OrderDirection), node.Attributes["Order"].Value);
            }

            return column;
        }

        public void ToXml(ref XmlNode root)
        {
            XmlDocument document = root.OwnerDocument;
            XmlNode node = document.CreateElement("Column");
            root.AppendChild(node);

            XmlAttribute attribute = document.CreateAttribute("Field");
            attribute.Value = this.Field;
            node.Attributes.Append(attribute);

            attribute = document.CreateAttribute("Visible");
            attribute.Value = this.Visible.ToString();
            node.Attributes.Append(attribute);

            attribute = document.CreateAttribute("Order");
            attribute.Value = Enum.GetName(typeof(OrderDirection), this.Order);
            node.Attributes.Append(attribute);
        }

        #endregion
    }
}
